import React, {Component} from 'react'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'
import {hashHistory} from 'react-router'
import {actions} from '../actions'
import Top from '../components/Top'
import Banner from '../components/Banner'
import Nav from '../components/Nav'
import Box from '../components/Box'
import HotGoods from '../components/HotGoods'
import WillLike from '../components/WillLike'
import Ad from '../components/Ad'
import Title from '../components/Title'
import ProductList from '../components/ProductList'
import Headline from '../components/Headline'
import Brand from '../components/Brand'
import Guide from '../components/Guide'
import GoodsItem from '../../global/components/GoodsItem'
import {forceCheck} from 'react-lazyload'

@connect(
	state => ({data: state.home.data}),
	dispatch => ({...bindActionCreators({fetchIndexData: actions.fetchIndexData}, dispatch)})
)

export default class App extends Component {
  constructor() {
    super()

    this.state = {isGuide: false}
  }

  componentWillMount() {

  }

  componentDidMount() {
    const {fetchIndexData, fetchCartCount, data, loadPosition} = this.props,
      fetchWechatShare = this.props.fetchWechatShare

    // data || fetchIndexData()
    // fetchCartCount()
    // fetchWechatShare()
	//
    // data && loadPosition('csxHome', '.wrapper')
    setTimeout(() => {
      forceCheck()
    }, 100)
  }

  componentWillUnmount() {
    this.props.savePosition('csxHome', '.wrapper')
  }

	// 扫码
  _scanStation = () => {
    wx.scanQRCode({
      needResult: 0,
      scanType: ['qrCode', 'barCode']
    })
  }

	// Banner跳转
  _jumpTo = link => {
    window.location.href = link
  }

	// 引导跳转
  _guide = () => {
    this.setState({isGuide: false})
    hashHistory.push('/cart')
  }

  render() {
    const {data, fetchAddToCart} = this.props

    if (data) {
      return (
        <section style={{height: '100%'}}>
            <section className="wrapper J-scroll">
                <Top
                    station={data.siteName}
                    scanStation={this._scanStation}
                />
                {
                    data.data.map((item, index) => item.type === 0 ? <Banner n={index} items={item.data} jumpTo={this._jumpTo} key={index}/> :
                        item.type === 1 ? <Nav items={item.data} jumpTo={this._jumpTo} key={index}/> :
                        item.type === 2 ? <HotGoods items={item.data} fetchAddToCart={fetchAddToCart} key={index}/> :
                        item.type === 3 ? <WillLike items={item.data} fetchAddToCart={fetchAddToCart} key={index}/> :
                        item.type === 4 ? <ProductList items={item.data} fetchAddToCart={fetchAddToCart} key={index}/> :
                        item.type === 5 ? <Title title={item.data} key={index}/> :
                        item.type === 6 ? <Box items={item.data} jumpTo={this._jumpTo} key={index}/> :
                        item.type === 7 ? <Ad item={item.data} jumpTo={this._jumpTo} key={index}/> : null
                    )
                }
            </section>
            {this.state.isGuide && <Guide handle={this._guide}/>}
        </section>
      )
    } else {
      return (
            <section>
                {/*<GoodsItem from= 'category'/>*/}
            </section>
      )
    }
  }
}
